package com.nengchuan.kbms.dfs.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nengchuan.config.exception.NcException;
import com.nengchuan.model.model.NcPageInfo;
import com.nengchuan.kbms.dfs.constant.BaseConstant;
import com.nengchuan.kbms.dfs.dao.DemoMapper;
import com.nengchuan.kbms.dfs.entity.FeignDemoDTO;
import com.nengchuan.kbms.dfs.entity.dto.DemoDTO;
import com.nengchuan.kbms.dfs.entity.dto.DemoPageDTO;
import com.nengchuan.kbms.dfs.entity.po.Demo;
import com.nengchuan.kbms.dfs.entity.vo.DemoPageVO;
import com.nengchuan.kbms.dfs.entity.vo.DemoVO;
import com.nengchuan.util.MapperUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 示例表 服务实现类
 * </p>
 *
 * @author luoping
 * @since 2020-03-19
 */
@Service
public class DemoServiceImpl extends ServiceImpl<DemoMapper, Demo> implements DemoService {

    @Override
    public Long insert(DemoDTO demoDTO) {
        Demo demo = MapperUtils.mapping(demoDTO, Demo.class);
        boolean save = this.save(demo);
        if (save) {
            return demo.getId();
        }
        throw new NcException("保存失败");
    }

    @Override
    public Boolean update(DemoDTO demoDTO) {
        Demo demo = MapperUtils.mapping(demoDTO, Demo.class);
        return this.updateById(demo);
    }

    @Override
    public Boolean delete(Long id) {
        return this.removeById(id);
    }

    @Override
    public DemoVO get(Long id) {
        Demo demo = this.getById(id);
        return MapperUtils.mapping(demo, DemoVO.class);
    }

    @Override
    public NcPageInfo<DemoPageVO> page(DemoPageDTO demoPageDTO) {
        IPage<Demo> page = this.page(new Page<>(demoPageDTO.getPageNum(), demoPageDTO.getPageSize()));
        NcPageInfo<DemoPageVO> ncPage = MapperUtils.mappingNcPage(page, DemoPageVO.class);
        return ncPage;
    }

    @Override
    public List<FeignDemoDTO> listByIds(String ids) {
        String[] idArr = ids.split(BaseConstant.SplitLetter.COMMA);
        List<String> idList = Arrays.asList(idArr);
        List<Demo> demoList = (List<Demo>) this.listByIds(idList);
        List<FeignDemoDTO> feignDemoDTOS = MapperUtils.mappingList(demoList, FeignDemoDTO.class);
        return feignDemoDTOS;
    }
}
